package com.arvato.right.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.common.redis.RedisUtil;
import com.arvato.common.util.ContantFinalUtil;
import com.arvato.common.util.MD5Util;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.controller.BaseController;
import com.arvato.right.bean.bean_do.WxworkRightUserDo;
import com.arvato.right.bean.bean_vo.WxworkRightUserVo;
import com.arvato.right.service.WxworkRightUserService;

/***
 * 权限管理：定死一些用户角色资源不可被删除编辑。在此前提下再开发权限管理。
 * 处理和用户信息相关的controller
 * @author smallking
 */
@Controller
@RequestMapping("/admin/right/user")
public class RightUserController extends BaseController{
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private WxworkRightUserService wxworkRightUserService;
	/**
	 * 获取当前用户
	 * @param req
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/getCurrUser",method=RequestMethod.POST)
	public Result getCurrUser(HttpServletRequest req) {
		WxworkRightUserVo user = (WxworkRightUserVo)req.getSession().getAttribute("loginUser");
		Result result = new Result();
		result.setData(user);
		return result;
	}
	
	/**
	 * 根据条件查询多个用户
	 * @param param
	 * param 中的参数 ：departmentId
	 * 				    start
	 *  				limit
	 *  				roleId
	 *  			或者keyword
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public PageData listUserByQuery(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData data = wxworkRightUserService.listUserByQuery(query, query.getPageNum(), query.getPageSize());
		return data;
	}
	
	/**
	 * 查询一个用户
	 * @param param
	 * param
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/detail",method=RequestMethod.POST)
	public Result getUserById(String userId){
		Result result = new Result(true);
		WxworkRightUserVo WxworkRightUserVo = wxworkRightUserService.getUserById(userId);
		result.setData(WxworkRightUserVo);
		return result;
	}
	
	/**
	 * 新增一个用户
	 * @param user
	 * @param roleIds 用户的角色组成的数组
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public Result insert(HttpServletRequest req, WxworkRightUserDo user, String[] roleIds){
		Query query = new Query();
		query.put("account", user.getAccount());
		user.setPassword(MD5Util.md5(user.getPassword()));
		// 新增之前 先看看 此用户是否已经存在
		WxworkRightUserVo checkUser = wxworkRightUserService.getUserByQuery(query);
		if(checkUser != null){
			return Result.fail("该账号已存在");
		}
		if(user.getStatus() == null) {
			user.setStatus(1);
		}
		user.setLastoperType(4);
		user.setLastoperDate(new Date());
		user.setCreateDate(new Date());
		try{
			int row = wxworkRightUserService.insert(user, roleIds);
			if(row == 1){
				return Result.success("新增成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e);
		}
		return Result.fail("新增失败");
	}
	
	/**
	 * 更新用户信息(包含更新用户头像)
	 * @param user
	 * @param userBrithday
	 * @param roleIds
	 * @return
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@ResponseBody 
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public Result update(WxworkRightUserDo user, String[] roleIds, String userBrithday, HttpServletRequest req) throws IllegalArgumentException, IllegalAccessException{
		if(ContantFinalUtil.MAX_USER_ID.equals(user.getUserId())) {
			return Result.fail("超级管理员无法修改。");
		}
		// 不给通过这儿修改密码
		user.setPassword(null);
		try{
			int row = wxworkRightUserService.update(user, roleIds);
			if(row == 1){
				return Result.success("修改成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("修改失败");
	}
	
	/**
	 * 修改用户密码
	 * @param req
	 * @param oldPassword
	 * @param newPassword
	 * @param userId
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@ResponseBody 
	@RequestMapping(value="/rpassword",method=RequestMethod.POST)
	public Result resetPassword(HttpServletRequest req, String oldPassword, String newPassword, String userId) throws IllegalArgumentException, IllegalAccessException{
		WxworkRightUserVo userVo = wxworkRightUserService.getUserById(userId);
		oldPassword = MD5Util.md5(oldPassword);
		if(!oldPassword.equals(userVo.getPassword())){
			return Result.fail("原密码不正确");
		}
		WxworkRightUserDo userDo = new WxworkRightUserDo();
		userDo.setUserId(userId);
		userDo.setPassword(MD5Util.md5(newPassword));
		try{
			int row = wxworkRightUserService.update(userDo);
			if(row == 1) {
				return Result.success("修改成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("修改失败");
	}
	
	/**
	 * 删除用户
	 * @param userId
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Result delete(HttpServletRequest req, String userId){
		// 无法删除超级管理员
		if(ContantFinalUtil.MAX_USER_ID.equals(userId)) {
			return Result.fail("超级管理员无法删除。");
		}
		try{
			// 删除其实是修改用户状态
			int row = wxworkRightUserService.delete(userId);
			if(row == 1) {
				// 如果删除成功了 则请求当前用户的登录信息
				String key = MD5Util.md5(userId);
				redisUtil.delete(key);
				return Result.success("删除成功");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e);
		}
		return Result.fail("删除失败");
	}
}
